#!/bin/bash/python
# _*_ coding: utf-8 _*_
# file： tool_file_modify
# date-time: 2023/8/29-18:14
# auth:hard work
# description
import os


_w_count = 0

def read_write_file(path, date, model='r'):
    """tips: read model return cmds_lst"""
    cmds_lst = []
    r_count = 0
    w_count = 0
    try:
        if os.path.exists(path):
            print(f'file {path} exists, it is will be delete')
            os.remove(path)
            if model == "r":
                with open(path, "r", encoding="ISO-8859-1") as f:
                    for cmd in f.readlines():
                        cmds_lst.append(cmd)
                        r_count += 1
                    print("file read r_count:", r_count)
                return cmds_lst, r_count
            if model == "w":

                if date is None:
                    return "date is none"
                else:
                    with open(path, "w", encoding="utf-8") as f:
                        for line in date:
                            f.write(line + "\n")
                            w_count += 1
                            print("write count:", w_count)

                return "write done ..."
        else:
            if model == "r":
                with open(path, "r", encoding="ISO-8859-1") as f:
                    for cmd in f.readlines():
                        cmds_lst.append(cmd)
                        r_count += 1
                    print("file read r_count:", r_count)
                return cmds_lst, r_count
            if model == "w":

                if date is None:
                    return "date is none"
                else:
                    with open(path, "w", encoding="utf-8") as f:
                        for line in date:
                            f.write(line + "\n")
                            w_count += 1
                            print("write count:", w_count)

                return "write done ..."

    except FileNotFoundError as e:
        print(e)
        print("file not found")
        raise "read and write error !!!"
    except FileExistsError as e:
        print(e)
        print("file not found")
        raise "read and write error !!!"
    except IOError as e:
        print(e)


def save_file_to_local(path, data):
    w_count = 0
    with open(path, "w") as f:
        for line in data:
            f.writelines(line)
            # f.write(line + "\n")
            w_count += 1
            # print("write count:", w_count)
    return w_count
def read_to_local_file(path):
    _cmds_lst = []
    _r_count = 0
    with open(path, "r",encoding='iso8859-1') as f:
        for cmd in f.readlines():
            _cmds_lst.append(cmd)
            _r_count += 1
        print("file read r_count:", _r_count)
    return _cmds_lst, _r_count

if __name__ == '__main__':
    path = r'F:\Result\port_brief.txt'
    res, _ = read_to_local_file(path)
    port_lst = []
    for port_id in range(1, 21):
        port_lst.append(str(port_id))
    port_status = ['up','down']
    for i in res:

        if  i[-4:-2] in port_lst or i[-4:-2] in port_status or i[-6:-1] in port_status:
            print('port 10-20:', i[-4:-2])
            print('Operate status', i[-6:-1])
            print('Physical status', i[-4:-2])

        elif i[-3:-2] in port_lst:
            print('port 1-9 :', i[-3:-2])
            print('Operate status', i[-6:-1])
            print('Physical status', i[-4:-2])
